import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import store from '../store'
import Layout from '@/views/layout/index.vue'

export const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'LAYOUT_VIEW',
    component: Layout,
    meta: { isLogin: true, hidden: true },
    redirect: '/user'
  },
  {
    path: '/user',
    name: 'USER_MANAGE',
    component: Layout,
    meta: { title: '用户管理', icon: 'el-icon-s-tools', alwaysShow: true },
    redirect: '/user/info',
    children: [
      {
        path: 'info',
        name: 'USER_INFO',
        component: () => import('@/views/userInfo/index'),
        meta: { title: '用户信息', isLogin: true }
      }
    ]
  },
  {
    path: '/login',
    name: 'LOGIN',
    component: () => import('@/views/login/index.vue'),
    meta: { isLogin: false, hidden: true }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// router.beforeEach(async (to, from, next) => {
//   const getUserInfoRes: boolean = await store.dispatch('initUserInfo')
//   if (getUserInfoRes) {
//     if (to.path === '/login') {
//       next(from.path)
//     } else {
//       next()
//     }
//   } else {
//     if (to.path === '/login' || !to.meta.isLogin) {
//       next()
//     } else {
//       next('/login')
//     }
//   }
// })

export default router
